﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using System.Data;
using DevExpress.Web.Mvc;
using System.IO;
using Newtonsoft.Json;
using System.Reflection;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]
    public class AllowanceApplicationFormFrontController : AdminBaseController
    {
        //
        // GET: /AllowanceApplicationFormFront/
        [AuthorityActionFilter("m_AllowanceApplicationFormFront", Order = 1)]
        [LogPageActionFilter("申请预受理", Order = 2)]
        public ActionResult Index()
        {
            string strwhere = " FApplicationFormID ='-1'";
            BLL.AllowanceApplicationForm AllowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
            DataSet ds = AllowanceApplicationFormBLL.GetListAllowanceApplicationFormFront(strwhere);
            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");
            return View(ds.Tables[0]);
        }

        public ActionResult  Save(Model.AllowanceApplicationForm model)
        {
            try
            {
                model.FApplyAmount = decimal.Parse(Fetch.Post("hidFApplyAmount"));
                model.FApplyDate = DateTime.Now;
                model.FApplyName = Fetch.Post("FApplyName");
                model.FAuditAmount = decimal.Parse(Fetch.Post("hidFAuditAmount"));
                model.FEntityNo = GlobalConstant.ENTITYNO;
                string[] noList = model.FFileNo.Split('|');
                model.FSource = noList[1];
                model.FSourceNo = noList[0];
                model.FFinancePropNo = noList[4];
                model.FFileNo = Fetch.Post("FFileNo");
                model.FInsDate = DateTime.Now;
                model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                model.FInsIP = GenerateUtil.GetClientIPv4Address();
                model.FIsReturn = "N";
                model.FNote = Fetch.Post("FNote");
                model.FStatus = "0";
                model.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                model.YHZK = model.KHYH;
                model.KHYH = Fetch.Post("KHYH");
                model.NSRDZDAH = decimal.Parse(Fetch.Post("hidNSRDZDAH"));
                string[] name = Fetch.Post("comboBox").Split(';');
                model.NSRMC = name[0];
                BLL.AllowanceApplicationForm allowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
                ViewData["FApplicationFormID"] = allowanceApplicationFormBLL.Add(model);
                ViewData["jsAlert"] = "操作成功";
                ViewData["isshowje"] = Fetch.Post("hidisshowje");

            }
            catch(Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
                ViewData["data"] = "";
            }
            return AllowanceApplicationFormFrontEdit("","");
        }

        public string DelAllowanceApplicationFormFront(string FApplicationFormID)
        {
            string str = string.Empty;
            if (null != FApplicationFormID)
            {
                FApplicationFormID = Fetch.estimateString(FApplicationFormID, CheckGetEnum.Safety);
                BLL.AllowanceApplicationForm codeNameBll = new BLL.AllowanceApplicationForm();
                try
                {
                    codeNameBll.Delete(FApplicationFormID);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }

        public ActionResult AllowanceApplicationFormFrontEdit(string NSRDZDAH, string FFileNo)
        {
            if (NSRDZDAH != "")
            {
                BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
                Model.DJ_NSRXX d = DJ_NSRXXBLL.GetModel(decimal.Parse(NSRDZDAH), OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                ViewData["NSRDZDAH"] = d.NSRDZDAH;
                ViewData["NSRDZDAH2"] = d.NSRMC + ";" + d.NSRBM;
            }
            else
            {
                ViewData["NSRDZDAH"] = "";
            }
            ViewData["FFileNo"] = FFileNo;
            return View("AllowanceApplicationFormFrontEdit");
        }

        public string GetDJ_YHXXList(string NSRDZDAH)
        {
            long BH = 0;
            List<Model.cboDJ_YHXX> financePropList = new List<Model.cboDJ_YHXX>();
            if (long.TryParse(NSRDZDAH, out BH))
            {
                BLL.DJ_YHXX DJ_YHXXBLL = new BLL.DJ_YHXX();
                financePropList = DJ_YHXXBLL.GetCboList("a.NSRDZDAH=" + NSRDZDAH + " and a.YXBZ='Y'");
            }
            return JsonConvert.SerializeObject(financePropList);
        }

        public string GetAllowanceFileList(string NSRDZDAH)
        {
            long BH = 0;
            List<Model.cboAllowanceFile> financePropList = new List<Model.cboAllowanceFile>();
            if (long.TryParse(NSRDZDAH, out BH))
            {
                BLL.AllowanceFile DJ_YHXXBLL = new BLL.AllowanceFile();

                financePropList = DJ_YHXXBLL.GetCboList(NSRDZDAH);

            }
            return JsonConvert.SerializeObject(financePropList);
        }

        public string GetFDDBRMC(string NSRDZDAH)
        {
            long BH = 0;
            string FDDBRMC = "";
            if (long.TryParse(NSRDZDAH, out BH))
            {
                BLL.DJ_NSRXX DJ_YHXXBLL = new BLL.DJ_NSRXX();
                FDDBRMC = DJ_YHXXBLL.GetFDDBRMC(NSRDZDAH);
            }
            return FDDBRMC;
        }

        public string GetJe(string FSource, string FAllowanceFileID, string NSRDZDAH)
        {
            BLL.AllowanceFile allowanceFileBLL = new BLL.AllowanceFile();
            string je = allowanceFileBLL.GetJe(FSource, FAllowanceFileID, NSRDZDAH);
            string jedx = "";
            if (je != "")
            {
                jedx = Common.Rmb.CmycurD(decimal.Parse(je));
            }
            return je + "|" + jedx;
        }

        public ActionResult LargeDataComboBoxPartial()
        {
            return View();
        }

        public ActionResult AllowanceApplicationFormFrontPrint(string FApplicationFormID, string isshowje)
        {
            ViewData["FApplicationFormID"] = FApplicationFormID;
            ViewData["isshowje"] = isshowje;
            return View();
        }

        public ActionResult AllowanceApplicationFormFrontPrintPartial()
        {
            string id = Request["FApplicationFormID"];
            string isshowje = Request["isshowje"];
            if (id != "")
            {
                BLL.AllowanceApplicationForm allowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
                Model.AllowanceApplicationForm model = allowanceApplicationFormBLL.GetModel(id);
                model.FApplyAmountDX = Common.Rmb.CmycurD(model.FApplyAmount);
                model.FAuditAmountDX = Common.Rmb.CmycurD(model.FAuditAmount);
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                PropertyInfo[] propertys = model.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    string tempName = pi.Name;
                    dt.Columns.Add(tempName);
                }

                DataRow dr = dt.NewRow();

                foreach (PropertyInfo pi in propertys)
                {
                    string tempName = pi.Name;
                    dr[tempName] = pi.GetValue(model, null);
                }
                if (isshowje == "true")
                {
                    dr["FAuditAmountDX"] = "";
                    dr["FAuditAmount"] = "";
                }
                dt.Rows.Add(dr);
                ds.Tables.Add(dt);
                ViewData["data"] = ds;
            }
            return PartialView("AllowanceApplicationFormFrontPrintPartial");
        }


        public ActionResult AllowanceApplicationFormFrontListPartial()
        {
            //int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            //int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            //int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            //int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            //string callbackArg = DevExpressHelper.CallbackArgument;

            //string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            //if (command == "PAGERONCLICK")
            //{
            //    string[] args = callbackArg.Split(new char[1] { '|' });
            //    string arg = args[args.Count() - 1];

            //    switch (arg)
            //    {
            //        case "PBN;":
            //            {
            //                //下一页
            //                pageIndex = pageIndex + 2;
            //                break;
            //            }
            //        case "PBP;":
            //            {
            //                //上一页
            //                //pageIndex = pageIndex;
            //                break;
            //            }
            //        case "PBL;":
            //            {
            //                //最后一页
            //                pageIndex = pageCount;
            //                break;
            //            }
            //        case "PBF;":
            //            {
            //                //第一页
            //                pageIndex = 1;
            //                break;
            //            }
            //        default:
            //            {
            //                //点击数字页码
            //                string tempIndex = arg.Replace("PN", "");
            //                tempIndex = tempIndex.Replace(";", "");
            //                if (RegExp.IsNumeric(tempIndex))
            //                {
            //                    pageIndex = int.Parse(tempIndex) + 1;
            //                }
            //                break;
            //            }

            //    }
            //}
            //else
            //{
            //    pageIndex = pageIndex + 1;
            //}

            string strwhere = " FApplicationFormID = '-1' ";

            string NSRMC = Request.Params["NSRMC"];
            string NSRBM = Request.Params["NSRBM"];
            string FFinancePropNo = Request.Params["FFinancePropNo"];
            string IsNoFull = Request.Params["IsNoFull"];

            if (IsNoFull != "")
            {
                if (IsNoFull == "1")
                {
                    strwhere = "  FApplicationFormID = '-1' ";
                }
                else
                {
                    strwhere = "  FApplicationFormID <> '-1' ";
                }
            }

            if (NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
            }
            if (NSRBM != "")
            {
                strwhere += " and NSRBM like '%" + NSRBM + "%'";
            }

            if (FFinancePropNo != "")
            {
                strwhere += " and FFinancePropNo='" + FFinancePropNo + "'";
            }

            BLL.AllowanceApplicationForm AllowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
            DataSet ds = AllowanceApplicationFormBLL.GetListAllowanceApplicationFormFront(strwhere);
            //List<Model.AllowanceApplicationForm> AllowanceApplicationFormList = new List<Model.AllowanceApplicationForm>();
            //AllowanceApplicationFormList = GetAllowanceApplicationFormListList(strwhere, pageIndex);
            return PartialView("AllowanceApplicationFormFrontListPartial", ds.Tables[0]);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.AllowanceApplicationForm> GetAllowanceApplicationFormListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.AllowanceApplicationForm AllowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
            string fiedls = "FApplicationFormID";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.t_AllowanceApplicationForm", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FApplicationFormID", out total);
            List<Model.AllowanceApplicationForm> AllowanceApplicationFormList = AllowanceApplicationFormBLL.DataTableToList(ds.Tables[0]);
            //foreach (Model.AllowanceApplicationForm a in AllowanceApplicationFormList)
            //{
            //    a.FApplyAmount = a.FApplyAmount / 10000;
            //    a.FAuditAmount = a.FAuditAmount / 10000;
            //}
            List<Model.AllowanceApplicationForm> AllowanceApplicationFormAllList = new List<Model.AllowanceApplicationForm>();
            total = AllowanceApplicationFormBLL.GetRecordCount(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                AllowanceApplicationFormAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (AllowanceApplicationFormList.Count > i)
                {
                    AllowanceApplicationFormAllList[((pageIndex - 1) * pageRowSize) + i] = AllowanceApplicationFormList[i];
                }
            }

            return AllowanceApplicationFormAllList;
        }

    }
}
